package com.foreknow.oop.hw4;

/**
 * 学生类
 */
public class Student {
    private int id;         // 学号
    private String name;    // 姓名
    private double score;   // 成绩

    public Student(int id, String name, double score) {
        this.id = id;
        this.name = name;
        this.score = score;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    /**
     * 按成绩将学生信息进行排序
     * @param students
     */
    public static void sort(Student[] students){
        for (int i = 0; i < students.length - 1; i++) {
            for (int j = 0; j < students.length - i - 1; j++) {
                if (students[j].score < students[j+1].score){
                    Student temp = students[j];
                    students[j] = students[j+1];
                    students[j+1] = temp;
                }
            }
        }
    }

    /**
     * 打印学生信息
     */
    public void showAll(){
        System.out.printf("%d\t%s\t%.2f\n",id,name,score);
    }

    public static void main(String[] args) {
        Student s1 = new Student(1001, "张三", 96.5);
        Student s2 = new Student(1002, "王二", 94.5);
        Student s3 = new Student(1003, "李四", 95.5);
        Student[] students = new Student[3];
        students[0] = s1;
        students[1] = s2;
        students[2] = s3;
        Student.sort(students);
        System.out.println("学号\t姓名\t成绩");
        for (Student stu : students) {
            stu.showAll();
        }
    }
}
